class Game < ActiveRecord::Base
  has_many :rounds
  belongs_to :player1,
    :class_name => "User",
    :foreign_key => "user_id"
  belongs_to :player2,
    :class_name => "User",
    :foreign_key => "user_id"

  def self.create_game_by_uid(player1_id,player2_id)
    @game = Game.new
    @game.player1 = User.find(player1_id)
    @game.player2 = User.find(player2_id)
    @game.score = 0
    return @game
  end

  def get_next_round
    @round = Round.new
    @round.picture = Picture.get_random_picture
    self.rounds << @round
    return @round
  end

  def close_and_save
    self.score = 0;
    self.rounds.each do |round|
      self.score += round.score
    end
    self.save
  end
end
